package com.lucy.disk.minio.provider;

import com.lucy.disk.constant.DiskConst;
import com.lucy.disk.domain.entity.FileEntity;
import com.lucy.disk.event.FileDeletedEvent;
import com.lucy.disk.minio.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author kindear
 * minio 事件消费机制
 */
@Service
public class MinioEventProvider {
    @Autowired
    MinioUtil minioUtil;

    // 订阅该事件
    @Async("threadExecutor")
    @EventListener(FileDeletedEvent.class)
    public void fileDeletedEvent(FileDeletedEvent event) throws Exception {
        FileEntity file = event.getFile();
        // 移除存储对象
        minioUtil.removeObject(DiskConst.DEFAULT_OSS_BUCKET_NAME,file.getOssUrl());
    }
}
